package com.tim.utils.common;

import com.dd.plist.NSArray;
import com.dd.plist.NSDictionary;
import com.dd.plist.NSObject;


public final class Log {

	private static final String TAG = "Debug";

	public static boolean DEBUG_ENABLED = true;
	
	public static void printNSDictionary(NSDictionary nDictionary) {
		if(DEBUG_ENABLED && !CommonUtil.isNull(nDictionary)) {
			for(String key : nDictionary.allKeys()) {
				final NSObject nObject = nDictionary.objectForKey(key);
				if(nObject instanceof NSArray) {
					printNSArray((NSArray) nObject);
				}
				else if(nObject instanceof NSDictionary) {
					printNSDictionary((NSDictionary) nObject);
					
				}
				else {
					Log.d(TAG, key + " = " + nDictionary.objectForKey(key));
				}
			}
		}
	}
	
	public static void printNSArray(NSArray nArray) {
		if(DEBUG_ENABLED && !CommonUtil.isNull(nArray)) {
			printArray(nArray.getArray());
		}
	}
	
	public static void printArray(Object[] array) {
		if(DEBUG_ENABLED && !CommonUtil.isNull(array)) {
			for(Object o : array) {
				Log.d(TAG, o.toString());
			}
		}
	}
	
	public static int i(String tag, Object msg) {
		return log(android.util.Log.INFO, tag, msg);
	}
	
	public static int d(String tag, Object msg) {
		return log(android.util.Log.DEBUG, tag, msg);
	}
	
	public static int v(String tag, Object msg) {
		return log(android.util.Log.VERBOSE, tag, msg);
	}
	
	public static int w(String tag, Object msg) {
		return log(android.util.Log.WARN, tag, msg);
	}
	
	public static int e(String tag, Object msg) {
		return log(android.util.Log.ERROR, tag, msg);
	}
	
	private static int log(int type, String tag, Object msg) {
		int returnValue = -1;
		if(!DEBUG_ENABLED) {
			return returnValue;
		}
		if(android.os.Build.VERSION.SDK_INT == 0) {	//Not Android environment
			System.out.println("" + msg);
			return returnValue;
		}
		switch (type) {
		case android.util.Log.DEBUG:
			returnValue = android.util.Log.d(tag, "" + msg);
			break;
			
		case android.util.Log.ERROR:
			returnValue = android.util.Log.e(tag, "" + msg);
			break;
			
		case android.util.Log.INFO:
			returnValue = android.util.Log.i(tag, "" + msg);
			break;
			
		case android.util.Log.VERBOSE:
			returnValue = android.util.Log.v(tag, "" + msg);
			break;
			
		case android.util.Log.WARN:
			returnValue = android.util.Log.w(tag, "" + msg);
			break;

		default:
			break;
		}
		return returnValue;
	}
}
